package com.cheetah.start.common.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.solon.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.solon.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

import javax.sql.DataSource;

/**
 * mybatis-plus 配置
 *
 * @author xjh
 * @date 2020/12/20 18:22
 */
@Configuration
public class MybatisPlusConfig {

    @Bean(value = "master", typed = true)
    public DataSource dataSource(@Inject("${jdbc.master}") HikariDataSource ds) {
        return ds;
    }

    @Bean
    public void masterConfig(@Db("master") org.apache.ibatis.session.Configuration cfg) {
        MybatisPlusInterceptor plusInterceptor = new MybatisPlusInterceptor();
        // 分页插件
        plusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 防止全表更新或删除插件
        plusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        cfg.setCacheEnabled(false);
        cfg.addInterceptor(plusInterceptor);
    }


}
